﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;
using EIA.PM.Model;

namespace EIA.SLClient.ProjectManage.Basic
{
	public partial class QueryConditionPanel : UserControl
	{
		public QueryConditionPanel()
		{
			// 为初始化变量所必需
			InitializeComponent();
            ViewModel.PropertyChanged += OnVMPropertyChanged;
		}

        private QueryConditionPanelViewModel ViewModel
        {
            get
            {
                QueryConditionPanelViewModel vm = (this.LayoutRoot.DataContext as QueryConditionPanelViewModel);
                return vm;
            }
        }

        #region Conditions (DependencyProperty)

        /// <summary>
        /// 该依赖属性的描述
        /// </summary>
        public Dictionary<string, object> Conditions
        {
            get { return (Dictionary<string, object>)GetValue(ConditionsProperty); }
            set { SetValue(ConditionsProperty, value); }
        }
        public static readonly DependencyProperty ConditionsProperty =
            DependencyProperty.Register("Conditions", typeof(Dictionary<string, object>), typeof(QueryConditionPanel),
            new PropertyMetadata(null, new PropertyChangedCallback(OnConditionsChanged)));

        private static void OnConditionsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            // 获取自身的引用
            QueryConditionPanel source = (QueryConditionPanel)sender;

            // 处理逻辑
            Dictionary<string, object> newValue = (Dictionary<string, object>)args.NewValue;
        }

        #endregion

        private void OnVMPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (isReseting) return;
            QueryConditionPanelViewModel vm = ViewModel;
            Dictionary<string, object> con = new Dictionary<string, object>();

            if(vm.XMLX != null)con[TProjectBasic.PRJ_TYPE] = vm.XMLX.DicIid;
            if (vm.XMJD != null) con[TProjectBasic._PROGRESS] = vm.XMJD.DicIid;
            if (!string.IsNullOrEmpty(vm.ConsOrg)) con[TProjectBasic.PRJ_CONS_ORG] = vm.ConsOrg;
            if (vm.XMLY != null) con[TProjectBasic.PRJ_SOURCE] = vm.XMLY.DicIid;
            if (vm.XMFZR != null) con[TProjectBasic.PRJ_LEADER] = vm.XMFZR.StfId;
            if (vm.HYLX != null) con[TProjectBasic.PRJ_INDUSTRY] = vm.HYLX.DicIid;
            if (vm.KPZT != null) con[TProjectBasic._INVOICE] = vm.KPZT.DicIid;
            if (vm.DKZT != null) con[TProjectBasic._RECEIPT] = vm.DKZT.DicIid;
            if (vm.HTZT != null) con[TProjectBasic._CONTRACT] = vm.HTZT.DicIid;
            if (vm.HTQDZT != null) con[TProjectContract.PRJ_CONTRACT_SIGNED_AS] = vm.HTQDZT.DicIid;
            //HTQDZT
            if (vm.XMSZD != null) con[TProjectBasic.PRJ_REGION] = vm.XMSZD.DicIid;
            if (vm.XMSPBM != null) con[TProjectBasic.PRJ_APPROVE_GOV] = vm.XMSPBM.DicIid;
            if (vm.WCSJ_0 != null) con[TProjectBasic.PRJ_SCHEDULED + "_0"] = vm.WCSJ_0.Value.ToString("yyyy-MM-dd");
            if (vm.WCSJ_1 != null) con[TProjectBasic.PRJ_SCHEDULED + "_1"] = vm.WCSJ_1.Value.ToString("yyyy-MM-dd");
            if (vm.CJSJ_0 != null) con[TProjectBasic._DATE + "_0"] = vm.CJSJ_0.Value.ToString("yyyy-MM-dd");
            if (vm.CJSJ_1 != null) con[TProjectBasic._DATE + "_1"] = vm.CJSJ_1.Value.ToString("yyyy-MM-dd");
            //WCSJ_0
            //WCSJ_1
            //CJSJ_0 
            //CJSJ_1 
            this.Conditions = con;
        }
        bool isReseting = false;
        public void Reset()
        {
            isReseting = true;
            QueryConditionPanelViewModel vm = ViewModel;
            vm.WCSJ_0 = null;
            vm.WCSJ_1 = null;
            vm.CJSJ_0 = null;
            vm.CJSJ_1 = null;
            vm.ConsOrg = null;

            vm.XMLX = null;
            vm.XMJD = null;
            vm.XMLY = null;
            vm.XMFZR = null;
            vm.HYLX = null;
            vm.KPZT = null;
            vm.DKZT = null;
            vm.HTZT = null;
            vm.HTQDZT = null;
            vm.XMSZD = null;
            vm.XMSPBM = null;

            vm.SelectedEngineer = null;
            vm.XMFZR = null;
            isReseting = false;
            this.Conditions = null;
        }
	}
}